<?php
/**
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     pmvc_formInput
 * Purpose:  renders form inputs
 * -------------------------------------------------------------
 */

use pmvc\template\smarty\SmartyUtil;

function smarty_function_pmvc_formInput(Array $params, &$smarty, $useRequestValue=true) {

	$request = SmartyUtil::getRegisteredObject($smarty, "Request", true);
	
	// add special attributes
	// type=
	if (!isset($params["type"])) {
		$params["type"] = "text";
	}
	// id=
	if (!isset($params["id"])
		&& isset($params["name"])) {
		$params["id"] = $params["name"];
	}
	// value=
	if (!isset($params["value"])
		&& $useRequestValue
		&& isset($params["name"])
		&& $request->hasParameter($params["name"])
		&& !is_array($request->getParameter($params["name"]))) {
		$params["value"] = $request->getParameter($params["name"]);
	}
	
	// render it
	$ret = '<input'.SmartyUtil::renderParamsAsAttributes($params, $smarty).' />';
    return SmartyUtil::returnOrAssign($smarty, $params, $ret);
}

?>